import 'dart:async';
import 'package:flutter/material.dart';
import 'package:harmonyhive_flutter/utils/http.dart';
import 'package:harmonyhive_flutter/utils/toast.dart';
import 'package:harmonyhive_flutter/utils/token.dart';

class LoginPage extends StatefulWidget {
  const LoginPage({super.key});

  @override
  State<LoginPage> createState() => _LoginPageState();
}

class _LoginPageState extends State<LoginPage> {
  // 定义两个控制器 获取输入框的值 - 注意:web端拉不起键盘
  final TextEditingController _phoneController = TextEditingController();
  final TextEditingController _codeController = TextEditingController();

  // 获取短信验证码
  void _getCode() async {
    // 获取输入框的值
    String mobile = _phoneController.text;

    // 对手机号进行校验
    if (mobile.isEmpty) {
      return ToastUtil.showError('请输入手机号');
    }

    RegExp reg = RegExp(r'^1[3-9]\d{9}$');
    if (!reg.hasMatch(mobile)) {
      return ToastUtil.showError('请输入合法的手机号');
    }
    // 开始倒计时
    _startCountdown();

    // 调用获取验证码接口
    var res = await http.get('/code', params: {'mobile': mobile});
    if (res['code'] != 10000) return ToastUtil.showError('获取验证码失败');

    // 验证码回写延迟处理
    Future.delayed(const Duration(seconds: 2), () {
      // 把验证码回显到验证码输入框中
      setState(() {
        _codeController.text = res['data']['code'];
      });
    });
  }

  // 4、登录业务
  void _login() async {
    // 4.1 获取输入框的值
    String mobile = _phoneController.text;
    String code = _codeController.text;
    // 4.2 对手机号进行校验
    if (mobile.isEmpty) {
      return ToastUtil.showError('请输入手机号');
    }

    RegExp reg = RegExp(r'^1[3-9]\d{9}$');
    if (!reg.hasMatch(mobile)) {
      return ToastUtil.showError('请输入合法的手机号');
    }
    // 4.3 对验证码进行校验
    if (code.isEmpty) {
      return ToastUtil.showError('请输入验证码');
    }

    RegExp reg1 = RegExp(r'^\d{6}$');
    if (!reg1.hasMatch(code)) {
      return ToastUtil.showError('请输入6位数字验证码');
    }
    // 4.4 调用登录接口
    var res = await http.post('login', data: {'mobile': mobile, 'code': code});
    if (res['code'] != 10000) return ToastUtil.showError('登录失败');
    // 4.5 存储token
    TokenManager tokenManager = TokenManager();
    await tokenManager.saveToken(res['data']['token']);
    // 4.6 清除定时器
    _timer?.cancel();
    // 4.7 返回上一页
    Navigator.pop(context);
  }

  // 1、获取短信验证码倒计时
  int _countdown = 60;
  bool _isCountingDown = false;
  Timer? _timer;
  void _startCountdown() {
    // 判断是否在倒计时 - 正在倒计时则返回
    if (_isCountingDown) {
      return;
    }
    // 2、设置开始倒计时
    _isCountingDown = true;
    // 3、开启倒计时
    _timer = Timer.periodic(const Duration(seconds: 1), (timer) {
      setState(() {
        if (_countdown > 0) {
          _countdown--;
        } else {
          // 销毁定时器
          _isCountingDown = false;
          _countdown = 60;
          timer.cancel();
        }
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      appBar: AppBar(
        title: const Text('登录'),
        backgroundColor: Colors.transparent,
        centerTitle: true,
      ),
      body: Container(
        padding: const EdgeInsets.all(20),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.start,
          children: [
            const Row(
              children: [
                Text(
                  '登录',
                  style: TextStyle(fontSize: 25, fontWeight: FontWeight.bold),
                ),
              ],
            ),
            const SizedBox(height: 10),
            const Row(
              children: [Text('加入享+, 让生活更轻松', style: TextStyle(fontSize: 15))],
            ),
            const SizedBox(height: 30),
            Row(
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                Expanded(
                  child: TextField(
                    controller: _phoneController,
                    keyboardType: TextInputType.phone,
                    maxLength: 11,
                    style: const TextStyle(fontSize: 16),
                    decoration: InputDecoration(
                      labelText: '手机号',
                      hintText: '请输入手机号',
                    ),
                  ),
                ),
                ElevatedButton(
                  style: ElevatedButton.styleFrom(
                    backgroundColor: Colors.white,
                    shadowColor: Colors.transparent,
                    foregroundColor: const Color.fromARGB(255, 85, 145, 175),
                    minimumSize: const Size(100, 50),
                  ),
                  onPressed: () {
                    _getCode();
                  },
                  child: _isCountingDown
                      ? Text(
                          '$_countdown s后重新获取',
                          // 设置倒计时时间样式
                          style: const TextStyle(color: Colors.grey),
                        )
                      : const Text('获取验证码'),
                ),
              ],
            ),
            const SizedBox(height: 8),
            TextField(
              controller: _codeController,
              keyboardType: TextInputType.number,
              maxLength: 6,
              style: const TextStyle(fontSize: 16),
              decoration: InputDecoration(
                labelText: '验证码',
                hintText: '请输入6位验证码',
              ),
            ),
            const SizedBox(height: 8),
            const Row(
              children: [
                Text(
                  '未注册手机号经验证后将自动登录',
                  style: TextStyle(fontSize: 12, color: Colors.grey),
                ),
              ],
            ),
            const SizedBox(height: 50),
            Row(
              children: [
                Expanded(
                  child: ElevatedButton(
                    style: ElevatedButton.styleFrom(
                      backgroundColor: const Color.fromARGB(255, 85, 145, 175),
                      minimumSize: const Size(100, 50),
                    ),
                    onPressed: () {
                      _login();
                    },
                    child: const Text(
                      '登录',
                      style: TextStyle(color: Colors.white, fontSize: 20),
                    ),
                  ),
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }
}
